// pages/user/register/index/form/form.js
import Notify from "@vant/weapp/notify/notify";
import {verifyInput, verifyPhone} from  "../../../../../utils/Util";
import {register} from "../../../../../service/User";
import {getStorageSync, setStorageSync} from "../../../../../utils/Storage";
import {getInfo} from "../../../../../service/User";

Component({
    /*
    * 接收外部样式
    * */
    options: {
        addGlobalClass: true
    },

    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        formData: {
            type: "",
            name: "",
            tel: "",
            code: "",
            password: "",
            re_password: ""
        },
        checked: true,
        agreement: {  ///协议
            show: false, //是否展示
            content: "" //数据
        },
        btnLoading: false
    },

    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () {
            this.getAgreementContent()
        },
    },


    /**
     * 组件的方法列表
     */
    methods: {
        /*
        * 获取缓存协议展示内容
        * */
        getAgreementContent: function () {
            let config = getStorageSync("config");
            let content = config.regist_agreement ? config.regist_agreement : "";
            content = content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ');
            this.setData({"agreement.content": content})
        },

        /*
        * input输入
        * */
        onInputChange: function (e) {
            let name = e.currentTarget.dataset.name;
            let val = e.detail.type || e.detail;
            if (name === "type" || name === "tel") {
                this.setData({["formData." + name]: val})
            } else {
                this.data.formData[name] = val
            }
        },

        /*
        * 同意规则验证
        * */
        onCheckboxChange: function () {
            this.setData({checked: !this.data.checked})
        },

        /*
        * 主协议弹框
        * */
        bindShowAgreement: function () {
            this.setData({"agreement.show": true})
        },
        onAgreementClose: function () {
            this.setData({"agreement.show": false})
        },

        /*
        * 内容提交
        * */
        onSubmit: async function () {
            try {
                const that = this;
                let formData = that.data.formData;
                let type = parseInt(formData.type);
                let params;
                if (!verifyInput(formData.type)) {
                    Notify({type: "warning", message: "请选择客户类型", context: this});
                    return false
                }
                if (type === 1) { //个人
                    if (!verifyInput(formData.name)) {
                        Notify({type: "warning", message: "真实姓名不能为空", context: this});
                        return false
                    } else if (!verifyInput(formData.id_card)) {
                        Notify({type: "warning", message: "身份证号码不能为空", context: this});
                        return false
                    } else if (!verifyInput(formData.tel)) {
                        Notify({type: "warning", message: "手机号码不能为空", context: this});
                        return false
                    } else if (!verifyPhone(formData.tel)) {
                        Notify({type: "warning", message: "手机号码格式不正确", context: this});
                        return false
                    } else if (!verifyInput(formData.code)) {
                        Notify({type: "warning", message: "短信验证码不能为空", context: this});
                        return false
                    }
                    params = {
                        type: 1,
                        name: formData.name,
                        id_card: formData.id_card,
                        tel: formData.tel,
                        code: formData.code
                    }
                } else {
                    if (!verifyInput(formData.org_name)) {
                        Notify({type: "warning", message: "机构全称不能为空", context: this});
                        return false
                    } else if (!verifyInput(formData.paper_num)) {
                        Notify({type: "warning", message: "统一社会信用代码不能为空", context: this});
                        return false
                    } else if (!verifyInput(formData.password)) {
                        Notify({type: "warning", message: "密码不能为空", context: this});
                        return false
                    } else if (!verifyInput(formData.re_password)) {
                        Notify({type: "warning", message: "确认密码不能为空", context: this});
                        return false
                    } else if (formData.re_password !== formData.password) {
                        Notify({type: "warning", message: "确认密码不一致", context: this});
                        return false
                    }
                    params = {
                        type: 2,
                        org_name: formData.org_name,
                        paper_type: 1, //营业执照号
                        paper_num: formData.paper_num,
                        password: formData.password,
                        re_password: formData.re_password
                    }
                }
                if (that.data.btnLoading) {
                    return false
                } else {
                    that.setData({btnLoading: true});
                    let result = await register(params);
                    let token = getStorageSync("token") || {};
                    setStorageSync("userType", type);
                    Object.assign(token, {
                        value: result.access_token, //token
                        times: parseInt(new Date().getTime() / 1000)  //生成时间
                    });
                    setStorageSync("token", token);
                    let userInfo = await getInfo();
                    setStorageSync("userInfo", userInfo);
                    wx.reLaunch({url: "/packageQuestionnaire/pages/list/list?from=register"});
                }
            } catch (e) {
                e.errCode ? Notify({type: "warning", message: e.errMsg, context: this}) : console.error(e);
            } finally {
                this.setData({btnLoading: false});
            }
        }
    }
})
